﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace IoTCM
{
    public abstract class HostBase : IHost
    {
        protected IObjectSerializer Serializer { get; private set; }
        public HostBase(IObjectSerializer serializer)
        {
            if(serializer == null) throw new ArgumentNullException();

            Serializer = serializer;
        }

        protected IThingProvider ThingProvider { get; private set; }
        public abstract string MimeType { get; }
        public abstract string Format { get; }

        public abstract string GetSerializedNameValuePair(string name, object value);

        public virtual string GetSerializedThings()
        {
            var rootThing = ThingProvider.GetThings();
            return Serializer.Serialize(rootThing);
        }

        public virtual string GetSerializedThing(string thingID)
        {
            var thing = ThingProvider.GetThings(thingID);
            if (thing == null) return null;

            return Serializer.Serialize(thing);
        }

        public virtual string GetSerializedValues()
        {
            var values = ThingProvider.GetValues();

            return Serializer.Serialize(values);
        }

        public virtual string GetSerializedValues(string id)
        {
            var valueList = ThingProvider.GetValues(id);

            if (valueList != null)
            {
                return Serializer.Serialize(valueList);
            }
            return null;
        }

        public virtual DataValue[] DeserializeValues(string serializedValues)
        {
            return Serializer.DeserializeValues(serializedValues).ToArray();
        }

        public virtual void SetThingProvider(IThingProvider provider)
        {
            ThingProvider = provider;
        }
    }
}
